package com.bw.zonghe0719.fragment.home

import androidx.lifecycle.viewModelScope
import com.bw.base.entity.CouponEntity
import com.bw.base.entity.GoodsEntity
import com.bw.base.entity.IndexEntity
import com.bw.base.entity.Res
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class HomeVieModel:BaseViewModel() {
    //首页数据
    val indexSuccess=SingleLiveData<Res<IndexEntity>>()
    val indexFail=SingleLiveData<String>()
    fun index(){
        viewModelScope.launch {
            try {
                indexSuccess.postValue(apiservice.index())
            }catch (e:Exception){
                indexFail.postValue(e.message.toString())
            }
        }
    }
    //商品
    val goodsSuccess=SingleLiveData<Res<GoodsEntity>>()
    val goodsFail=SingleLiveData<String>()
    fun indexGoods(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                goodsSuccess.postValue(apiservice.indexGoods(type,limit,page))
            }catch (e:Exception){
                goodsFail.postValue(e.message.toString())
            }
        }
    }
    //优惠券
    val couponSuccess=SingleLiveData<Res<CouponEntity>>()
    val couponFail=SingleLiveData<String>()
    fun coupon(limit:String,page:String,type:String){
        viewModelScope.launch {
            try {
                couponSuccess.postValue(apiservice.coupon(limit,page,type))
            }catch (e:Exception){
                couponFail.postValue(e.message.toString())
            }
        }
    }
    //领取优惠券
    val couponReceiveSuccess=SingleLiveData<Res<String?>>()
    val couponReceiveFail=SingleLiveData<String>()
    fun couponReceive(couponId:Int){
        viewModelScope.launch {
            try {
                var map= mutableMapOf<String,Any>()
                map.put("couponId",couponId)
                couponReceiveSuccess.postValue(apiservice.couponReceive(mapToBody(map)))
            }catch (e:Exception){
                couponReceiveFail.postValue(e.message.toString())
            }
        }
    }
}